const mysql = require('mysql2');
const fs = require('fs');

clearOutpu();

const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test',
  password: '123456',
  port: 3307,
});

connection.execute('SHOW TABLES;', (err, rows) => {
  if (err instanceof Error) {
    console.log('execute error:', err);
    return;
  }

  console.log('数据库test已启动');

  if (rows.length === 0) {
    connection.execute(
      'CREATE TABLE qq_list (qq char(20) NOT NULL, name char(10) NOT NULL) ENGINE=InnoDB;',
      (err, rows) => {
        if (err instanceof Error) {
          console.log('execute error:', err);
          return;
        }

        console.log('test数据库创建qq_list表成功');
      }
    );
  } else {
    connection.execute('DELETE FROM qq_list;', (err, rows) => {
      if (err instanceof Error) {
        console.log('execute error:', err);
        return;
      }

      console.log('test数据库, qq_list表已清空成功, 按 Ctrl+C 退出程序。');
    });
  }
});

function clearOutpu() {
  if (fs.existsSync('./输出.txt')) {
    fs.unlink('./输出.txt', function (err) {
      if (err) {
        console.log('删除 输出.txt失败:', err);
        return;
      }
      console.log('输出.txt已删除');
    });
  }
}
